import turtle
import math

def draw_square(pen, x, y, length):
    pen.penup()
    pen.goto(x - length / 2, y - length / 2)
    pen.setheading(0)  # 将绘制方向初始化为向右
    pen.pendown()
    for _ in range(4):
        pen.forward(length)
        pen.left(90)

def draw_board(pen, step_len, j):
    pen.color('black')
    pen.pensize(1)

    center_x = j * (4 * step_len)  # 棋盘的x坐标，每个棋盘的间隔为4个 step_len

    center_y = 0  # 棋盘的y坐标，保持为0

    # 绘制开始的三个方块
    for i in range(3):
        length_i = step_len * (i + 1)
        draw_square(pen, center_x, center_y, length_i)

    # 连接正方形的四边
    pen.penup()
    pen.goto(center_x - step_len / 2, center_y)
    pen.pendown()
    pen.setheading(180)
    pen.forward(step_len)

    pen.penup()
    pen.goto(center_x + step_len / 2, center_y)
    pen.pendown()
    pen.setheading(0)
    pen.forward(step_len)

    pen.penup()
    pen.goto(center_x, center_y - step_len / 2)
    pen.pendown()
    pen.setheading(270)
    pen.forward(step_len)

    pen.penup()
    pen.goto(center_x, center_y + step_len / 2)
    pen.pendown()
    pen.setheading(90)
    pen.forward(step_len)

    # 绘制四条线段
    pen.color('black')
    pen.pensize(1)
    diag_len = step_len * math.sqrt(2)  # 对角线长度
    dist_to_center = step_len / 2  # 到中心的距离
    
    pen.penup()
    pen.goto(center_x - dist_to_center, center_y + dist_to_center)
    pen.pendown()
    pen.setheading(135) # 左上角起点
    pen.forward(diag_len)

    pen.penup()
    pen.goto(center_x + dist_to_center, center_y + dist_to_center)
    pen.pendown()
    pen.setheading(45) # 右上角起点
    pen.forward(diag_len)

    pen.penup()
    pen.goto(center_x + dist_to_center, center_y - dist_to_center)
    pen.pendown()
    pen.setheading(-45) # 右下角起点
    pen.forward(diag_len)

    pen.penup()
    pen.goto(center_x - dist_to_center, center_y - dist_to_center)
    pen.pendown()
    pen.setheading(-135) # 左下角起点
    pen.forward(diag_len)

    # 绘制红色的方块
    pen.color('red')
    pen.pensize(3)
    draw_square(pen, center_x, center_y, 2 * step_len)


canvas = turtle.Screen()
pen = turtle.Turtle()

step_len = 50
draw_board(pen, step_len, -0.5)
draw_board(pen, step_len, 0.5)

canvas.exitonclick()
